#include "dicom/storage/stream.h"

namespace dicom {

Status FileReader::Open(const std::string& path) {
  return SequentialFile::Open(path, &in_file_);
}

int FileReader::Read(void* buf, size_t len) {
  size_t bytes;
  Status s = in_file_->Read(buf, len, &bytes);
  if (!s.ok()) return -1;
  return static_cast<int>(bytes);
}

void FileReader::Close() {
  if (in_file_.get()) {
    in_file_.reset();
  }
}

Status FileWriter::Open(const std::string& path) {
  return WritableFile::Open(path, true, &out_file_);
}

int FileWriter::Write(const void* buf, size_t len) {
  Status s = out_file_->Append(Slice(static_cast<const char*>(buf), len));
  if (!s.ok()) return -1;

  return static_cast<int>(len);
}

int FileWriter::Finalize() { return 0; }
void FileWriter::Close() {
  if (out_file_.get()) {
    out_file_.reset();
  }
}
}
